package com.itcpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcpeng.constant.MessageConstant;
import com.itcpeng.constant.RedisConstant;
import com.itcpeng.entity.PageResult;
import com.itcpeng.entity.QueryPageBean;
import com.itcpeng.entity.Result;
import com.itcpeng.pojo.Package;
import com.itcpeng.service.PackageService;
import com.itcpeng.util.QiNiuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping(value = "/package")
public class PackageController {

    @Reference
    private PackageService packageService;

    @Autowired
    private JedisPool jedisPool;

    //图片上传
    @PostMapping(value = "/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        try {
            //图片名称
            String originalFilename = multipartFile.getOriginalFilename();
            //产生唯一的key值
            UUID uuid = UUID.randomUUID();
            //文件扩展名
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String s = uuid + substring;
            //调用QiNiuUtil上传图片
            QiNiuUtil.uploadViaByte(multipartFile.getBytes(), s);
            //保存到redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, s);
            //封装返回的结果
            Map<String, String> map = new HashMap<String, String>();
            map.put("picName", s);
            map.put("domain", QiNiuUtil.DOMAIN);
            //返回结果
            return new Result(true, MessageConstant.UPLOAD_SUCCESS, map);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
    }

    //添加套餐
    @PostMapping(value = "/add")
    public Result add(@RequestBody Package pkg, Integer[] checkgroupIds) {
        //调用业务
        packageService.add(pkg,checkgroupIds);
        //保存图片到我的redis
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, pkg.getImg());
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //套餐分页
    @PostMapping(value = "/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageBeanPageResult = packageService.findPage(currentPage, pageSize, queryString);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pageBeanPageResult);
    }

    //删除
    @PostMapping(value = "/deleteAll")
    public Result deleteAll(Integer id) {
        packageService.deleteAll(id);
        return new Result(true, MessageConstant.DELETE_PACKAGE_SUCCESS);
    }

    //根据id查询
    @GetMapping(value = "/findById")
    public Result findById(Integer id) {
       Package packageIds = packageService.findById(id);
       return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,packageIds);
    }

    //根据id查询检查组
    @GetMapping(value = "/findpackagecheckgroupIds")
    public List<Integer> findPakageCheckgroupIds(Integer id) {
        List<Integer> list = packageService.findPakageCheckgroupIds(id);
        return list;
    }

    //编辑回显
    @PostMapping(value = "/edit")
    public Result edit(@RequestBody Package pkg, Integer[] checkgroupIds) {
        packageService.edit(pkg, checkgroupIds);
        return new Result(true, MessageConstant.EDIT_PACKAGE_SUCCESS);
    }

}
